<!DOCTYPE html>
<html lang="zh-cn">

    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/component/jsoneditor/css/jsoneditor.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />

    </head>

    <body>

        <form class="layui-form">

            <div class="mainBox">
                <div class="main-container mr-5">

                    <div class="layui-form-item">
                        <label class="layui-form-label required">文章标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" value="" required lay-verify="required" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">文章描述</label>
                        <div class="layui-input-block">
                            <textarea name="description" required lay-verify="required"
                                class="layui-textarea"></textarea>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">文章分类</label>
                        <div class="layui-input-block">
                            <div name="cate_name" id="cate_name" required value=""></div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">是否置顶</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="istop" lay-filter="istop" lay-skin="switch" />
                            <input type="text" style="display:none" name="istop" value="0" />
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">文章内容</label>
                        <div class="layui-input-block">
                            <textarea id="content" name="content" required lay-verify="required"
                                class="layui-textarea"></textarea>
                        </div>
                    </div>



                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>

        </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/component/jsoneditor/jsoneditor.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>

        <script>
            // 分类列表
            var cateList = [];

            // 相关接口
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/blog-article/select" + location.search;
            const UPDATE_API = "/app/admin/blog-article/update";

            // 获取数据库记录
            layui.use(["form", "util", "popup"], function () {
                let $ = layui.$;
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    success: function (res) {
                        var initCateNmae = '';

                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="' + key + '"]');
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(value);
                            }
                            if (obj[0].id === "cate_name") {
                                // 初始分类名称
                                initCateNmae = value;
                            }
                            else {
                                obj.attr("value", value);
                                obj[0].value = value;
                            }
                        });

                        // 字段 文章内容 content
                        layui.use(["tinymce"], function () {
                            var tinymce = layui.tinymce
                            var edit = tinymce.render({
                                elem: "#content",
                                height: 560,
                                width: '100%',
                                'lay-verify': "required",
                                images_upload_url: "",
                            });
                            edit.on("blur", function () {
                                layui.$("#content").val(edit.getContent());
                            });
                        });

                        // 字段 是否置顶 istop
                        layui.use(["form"], function () {
                            layui.$("#istop").attr("checked", layui.$('input[name="istop"]').val() != 0);
                            layui.form.render();
                            layui.form.on("switch(istop)", function (data) {
                                layui.$('input[name="istop"]').val(this.checked ? 1 : 0);
                            });
                        })

                        // 字段 文章分类 cate_name
                        layui.use(["xmSelect"], function () {
                            layui.$.get("/app/admin/blog-category/list", function (res) {
                                cateList = res.data;

                                let data = [];
                                if (res.code == 200) {
                                    data = res.data.map(item => {
                                        return {
                                            name: item.name,
                                            value: item.name,
                                        }
                                    });
                                }
                                layui.xmSelect.render({
                                    el: "#cate_name",
                                    name: "cate_name",
                                    filterable: false,
                                    data: data,
                                    initValue: [{
                                        value: initCateNmae,
                                        name: initCateNmae,
                                    }],
                                    radio: true,
                                    clickClose: true,
                                    layVerify: "required",
                                })
                            });
                        });


                        // ajax返回失败
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }

                    }
                });
            });

            //提交事件
            layui.use(["form", "popup"], function () {
                layui.form.on("submit(save)", function (fieldsObj) {
                    var data = fieldsObj.field;
                    data[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    data['cate_id'] = cateList.find(item => item.name == data['cate_name']).id;

                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>

</html>